package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.TravelItemDao;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = TravelItemService.class)//需要使用dubbo框架，用于发布一个服务
@Transactional//给所有业务添加事务
public class TravelItemServiceImpl implements TravelItemService {
    @Autowired
    private TravelItemDao travelItemDao;

    @Override
    public void add(TravelItem travelItem) {
        travelItemDao.add(travelItem);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //1、开启分页功能
        PageHelper.startPage(currentPage,pageSize);//开启分页功能，limit ?,?
        //Page  extends ArrayList
        Page page=travelItemDao.findPage(queryString);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void delete(Integer id) {
        int count=travelItemDao.findTravelItemById(id);//从中间表查找关联数据，如果存在，就不进行删除自由行
        if(count>0){
            throw new RuntimeException("存在关联数据，无法删除，请先解除关系在进行删除");

        }
        travelItemDao.delete(id);
    }

    @Override
    public TravelItem get(Integer id) {
        return travelItemDao.get(id);
    }

    @Override
    public void edit(TravelItem travelItem) {
        travelItemDao.edit(travelItem);
    }

    @Override
    public List<TravelItem> findAll() {
        return travelItemDao.findAll();
    }
}
